planAndPostOptions = function($)
{
  var init = function()
  {
    var maleBDText = $("#settings textarea[name='defaultBDMessageMale']");
    var femaleBDText = $("#settings textarea[name='defaultBDMessageFemale']");
    $('.optionsBtn').click(openOptions);
    $("#saveSettings").click(saveSettings);
    $("#equalGenders").click(handleEqualGenders);
    maleBDText.keyup(equateGenders);
    $("#settings textarea").keyup(checkForReplacements);
    if (maleBDText.val() == femaleBDText.val())
    {
      femaleBDText.attr("disabled", "disabled");
      $("#equalGenders").attr("checked","checked");
    }
  }, checkForReplacements = function()
  {
    $(this).next().filter(".preview").remove();
    var friends = faceBook.getFriends();
    var friend;
    if (typeof (friends[0] == "object"))
    {
      friend = friends[0];
    }
    else
    {
      friend = {
        first_name : "John",
        last_name : "Smith"
      };
    }
    if ($(this).val().match("\\*(FIRST_NAME|LAST_NAME)\\*"))
    {
      $(this).after(
          "<div class='preview'>Example: "
              + $(this).val().replace("*FIRST_NAME*", friend.first_name)
                  .replace("*LAST_NAME*", friend.last_name) + "</div>");
    }
  }, handleEqualGenders = function()
  {
    var maleBDText = $("#settings textarea[name='defaultBDMessageMale']");
    var femaleBDText = $("#settings textarea[name='defaultBDMessageFemale']");
    if ($(this).is(":checked"))
    {
      femaleBDText.val(maleBDText.val());
      femaleBDText.attr("disabled", "disabled");
    }
    else
    {
      femaleBDText.removeAttr("disabled");
    }
  }, equateGenders = function()
  {
    if ($("#equalGenders").is(":checked"))
    {
      var femaleBDText = $("#settings textarea[name='defaultBDMessageFemale']");
      femaleBDText.val($(this).val());
    }
  }, openOptions = function(event)
  {
    $('#settings').dialog({
      minWidth : 600,
      title : 'Settings',
      dialogClass : "dialogWithDropShadow",
      modal : true,
      position :
      [ event.clientX - 600, event.clientY + 10 ]
    });
  }, saveSettings = function()
  {
    var data = {
      accessToken : FB._authResponse.accessToken,
      userid : faceBook.getUser().id
    };
    $("#settings :input[name]").each(function()
    {
      if ($(this).is(":text,:radio:checked,textarea"))
      {
        data[$(this).attr("name")] = $(this).val();
      }
    });
    $.ajax({
      url : "/options",
      data : data,
      dataType : "json",
      type : "post",
      success : function(data)
      {
        if (data.success)
        {
          $('#settings').dialog("close");
          faceBook.login();
        }
        else
        {
          if (data.error)
          {
            alert(data.error)
          }
          else
          {
            alert("Unknown error");
          }
        }
      },
      error : function()
      {
        alert("Unknown error");
      }
    });
  };

  return {
    init : init
  };
}(jQuery);

$(function()
{
  planAndPostOptions.init();
});